package com.example.myandroidapp1;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

import com.example.myandroidapp1.activity.AbstractBaseActivity;

public class Accelerate extends AbstractBaseActivity implements
		SensorEventListener {

	private float x, y, sensorX, sensorY;
	private Bitmap ball;
	private MyGFXSurfaceView mySurfaceView;
	private SensorManager sm ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mySurfaceView = new MyGFXSurfaceView(this);
		setContentView(mySurfaceView);
		sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
			Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
			sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
		}
		ball = BitmapFactory.decodeResource(getResources(),
				R.drawable.green_ball);
		x = y = sensorX = sensorY = 0;
		mySurfaceView.resume();

	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent arg0) {
		// TODO Auto-generated method stub
		sensorX = arg0.values[0];
		sensorY = arg0.values[1];
		mySurfaceView.setSensorX(sensorX);
		mySurfaceView.setSensorY(sensorY);
		try {
			Thread.sleep(16);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		sm.unregisterListener(this);
	}
}
